(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.companyController', companyController);
	companyController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig',
	                              'validatorConfig', 'authService', 'ajaxServiceFactory', '$filter'];
	function companyController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, 
			validatorConfig, authService, ajaxServiceFactory, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("companyLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getCountry: {
				url : 'do/cda/dataList/country',
				method : 'get'
			},
			getCompanyType: {
				url : 'do/cda/dataList/companyOptions',
				method : 'get'
			}
		});
		
		function getCountryOptions() {
			var defer = $q.defer();
			ajaxService.getCountry().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getCompanyTypeOptions() {
			var defer = $q.defer();
			ajaxService.getCompanyType().then(function(response) {
				var companyOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue.data = value;
						companyOptions[key] = tempValue;
					});
				}
				defer.resolve(companyOptions);
			});
			return defer.promise;
		}
		
		function initDataOptions(ctrl){
			getCompanyTypeOptions().then(function(response) {
				ctrl.companyTypeOptions = ctrl.companyTypeOptions == null ? response.companyTypeOptions : ctrl.companyTypeOptions;
				ctrl.companyTypeOptions.hasEmptyOption = true;
			}, angular.noop);
			getCountryOptions().then(function(response) {
				ctrl.companyCountryOptions = response;
				ctrl.companyCountryOptions.hasEmptyOption = true;
			});
		}
		
		ctrl.ok = function() {
			if(dialogsModule.params.isReportCompanyChanging){
				if(dialogsModule.params.companyCode.length == 1 || $filter('encode')(ctrl.selectedRows[0].companyCode) != $filter('encode')(dialogsModule.params.companyCode)){
					modalService.openConfirmDialogs({
						'messages' :  [ 'title.dialog.confirm.continue'],
						'btnCallback' : {
							'ok' : function() {
								if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
									$uibModalInstance.close();
								}
							}
						}
					});	
				}else{
					if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
						$uibModalInstance.close();
					}
				}
			}
			else{
				if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
					$uibModalInstance.close();
				}
			}
			
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.gridOptions.multiPageSelect = true;
			if (ctrl.dialogsModule.params.multiSelect) {
				ctrl.gridOptions.multiSelect = true;
			}
			if (ctrl.dialogsModule.params.multiSelect == "false") {
				ctrl.gridOptions.multiSelect = false;
			}
			if(ctrl.dialogsModule.params.isReportCompany){
				ctrl.searchCriteria.isReportCompany = ctrl.dialogsModule.params.isReportCompany;
				if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.serviceCode) {
					ctrl.searchCriteria.serviceCode = ctrl.dialogsModule.params.serviceCode;
				}
				if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.filterCompanyTypes) {
					ctrl.searchCriteria.companyTypeList = ctrl.dialogsModule.params.filterCompanyTypes;
				}
			}
			if(ctrl.dialogsModule.params.workingCompanyCode){
				ctrl.searchCriteria.workingCompanyCode = ctrl.dialogsModule.params.workingCompanyCode;
			}
			if (ctrl.dialogsModule.params.companyCode) {
				ctrl.searchCriteria.companyCodeSearch = $filter('encode')(ctrl.dialogsModule.params.companyCode);// get params from parent page, for filter
				ctrl.layoutConfig.formConfigs.searchCriteria.gridConfigs['companyCode'].placeholder = $filter('encode')(ctrl.dialogsModule.params.companyCode);
			} else {
				ctrl.searchCriteria.companyCode = '';
			}
			if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.companyTypeReadonly) {
				ctrl.companyTypeReadonly = true;
			} else {
				ctrl.companyTypeReadonly = false;
			} 
			if (ctrl.dialogsModule.params.companyType) {
				ctrl.searchCriteria.companyType = ctrl.dialogsModule.params.companyType;// get params from parent page
			} else {
				ctrl.searchCriteria.companyType = '';
			}
			//get params from parent page, used for super site update subCompany(companyType = BANK, while companyType search field is readonly)
			if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.companyTypeReadOnlyForBank) {	
				if ( "SYSADM" == ctrl.dialogsModule.params.companyTypeReadOnlyForBank){
					ctrl.companyTypeOptions = [];
					ctrl.companyTypeOptions.data = ctrl.companyTypeOptions.data || [];
					ctrl.companyTypeOptions.data.push({label : "BANK", value : "BANK"});
					ctrl.companyTypeReadonly = true;
				} else {
					ctrl.companyTypeReadonly = false;
				}
			}
			if (ctrl.dialogsModule.params.filterCompanyTypes) {
				ctrl.searchCriteria.filterCompanyTypes = ctrl.dialogsModule.params.filterCompanyTypes;// get params from parent page
			} else {
				ctrl.searchCriteria.filterCompanyTypes = [];
			}
			if(ctrl.dialogsModule.params.excludeBank){
				ctrl.searchCriteria.excludeBank = ctrl.dialogsModule.params.excludeBank;
			}
			if (ctrl.dialogsModule.params.action) {
				ctrl.searchCriteria.action = ctrl.dialogsModule.params.action;// get params from parent page
			} else {
				ctrl.searchCriteria.action = '';
			}
			if (ctrl.dialogsModule.params.isSite) {
				ctrl.searchCriteria.isSite = ctrl.dialogsModule.params.isSite;// get params from parent page
			} else {
				ctrl.searchCriteria.isSite = '';
			}
			if (ctrl.dialogsModule.params.filterCompanyCodes) {
				var resultArr = new Array();
				angular.forEach(ctrl.dialogsModule.params.filterCompanyCodes, function(code, key) {
					var resultPropVal = $filter('encode')(code);
					if(resultPropVal) resultArr.push(resultPropVal);
				});
				ctrl.searchCriteria.filterCompanyCodes = resultArr;
			} else if(ctrl.dialogsModule.params.filterCompanyCodesForBu) {
				var resultArr = new Array();
				angular.forEach(ctrl.dialogsModule.params.filterCompanyCodesForBu, function(obj, key) {
					var resultPropVal = $filter('encode')(obj['buCtrlData']);
					if(resultPropVal) resultArr.push(resultPropVal);
				});
				ctrl.searchCriteria.filterCompanyCodes = resultArr;
			} else {
				ctrl.searchCriteria.filterCompanyCodes = [];
			}
			if (ctrl.dialogsModule.params.filterCompanyCode) {
				ctrl.searchCriteria.filterCompanyCode = $filter('encode')(ctrl.dialogsModule.params.filterCompanyCode);
			} else {
				ctrl.searchCriteria.filterCompanyCode = '';
			}
			if (ctrl.dialogsModule.params.lookupFlag) {
				ctrl.searchCriteria.lookupFlag = ctrl.dialogsModule.params.lookupFlag;// get params from parent page
			}
			initDataOptions(ctrl);
			ctrl.search();
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			ctrl.searchCriteria.parameters=[];
			if(ctrl.dialogsModule.params.companyTypes){
				ctrl.searchCriteria.parameters=ctrl.dialogsModule.params.companyTypes;
			};
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
